﻿using AwesomeChromeFormUI.Utils;
using CefSharp;
using CefSharp.DevTools;
using CefSharp.DevTools.Page;
using CefSharp.WinForms;
using System;
using System.Drawing.Imaging;
namespace AwesomeChromeFormUI.Handlers
{
    /// <summary>
    /// 自定义右键菜单
    /// </summary>
    public class DefaultContextMenuHandler : IContextMenuHandler
    {
        /// <summary>
        /// 上下文菜单列表，在这里加菜单
        /// </summary>
        /// <param name="chromiumWebBrowser"></param>
        /// <param name="browser"></param>
        /// <param name="frame"></param>
        /// <param name="parameters"></param>
        /// <param name="model"></param>
        void IContextMenuHandler.OnBeforeContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
        {
            if (model.Count > 0)
            {
                // 添加分隔符
                model.AddSeparator();
            }
            model.AddItem((CefMenuCommand)29501, "Show DevTools");
            model.AddItem((CefMenuCommand)29502, "Reload");
            model.AddItem((CefMenuCommand)29503, "Save Screenshot");
        }

        /// <summary>
        /// 上下文菜单指令，这里实现菜单要做的事情
        /// </summary>
        /// <param name="chromiumWebBrowser"></param>
        /// <param name="browser"></param>
        /// <param name="frame"></param>
        /// <param name="parameters"></param>
        /// <param name="commandId"></param>
        /// <param name="eventFlags"></param>
        /// <returns></returns>
        bool IContextMenuHandler.OnContextMenuCommand(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
        {
            if (commandId == (CefMenuCommand)29501)
            {
                browser.GetHost().ShowDevTools();
                return true;
            }
            if (commandId == (CefMenuCommand)29502)
            {
                browser.Reload(ignoreCache: true);
                return true;
            }
            if (commandId == (CefMenuCommand)29503)
            {
                SaveScreenshotAsync(chromiumWebBrowser.GetDevToolsClient());
                return true;
            }
            return false;
        }

        void IContextMenuHandler.OnContextMenuDismissed(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
        {
            var webBrowser = (ChromiumWebBrowser)chromiumWebBrowser;
            Action setContextAction = delegate ()
            {
                webBrowser.ContextMenu = null;
            };
            webBrowser.Invoke(setContextAction);
        }

        bool IContextMenuHandler.RunContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
        {
            // 必须返回 false
            return false;
        }

        /// <summary>
        /// 保存屏幕快照
        /// </summary>
        private async void SaveScreenshotAsync(DevToolsClient devToolsClient)
        {
            CaptureScreenshotResponse result = await devToolsClient.Page.CaptureScreenshotAsync();
            byte[] imageData = result.Data;
            ImageConverter.ByteArrayToImage(imageData, "output.png", ImageFormat.Png);
        }
    }
}
